import { Injectable } from '@angular/core';
import * as gc from 'src/app/global/const';
import { JsonResult, ZorroOption } from 'src/app/global/model';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

@Injectable({
  providedIn: 'root'
})
export class ContactTypeService {

  path_pre = 'contactType';
  url = {
    'deleteByPrimaryKey': gc.BACKEND_SERVER_ROOT + '/' + this.path_pre,
    'selectByPrimaryKey': gc.BACKEND_SERVER_ROOT + '/' + this.path_pre,
    'saveOne': gc.BACKEND_SERVER_ROOT + '/' + this.path_pre,
    'selectAll': gc.BACKEND_SERVER_ROOT + '/' + this.path_pre + '/a',
  }

  constructor(
    private http:HttpClient
  ) { }

  selectAll():Observable<JsonResult>{
    return this.http.get<JsonResult>(this.url.selectAll);
  }

  deleteByPrimaryKey(iid:number):Observable<JsonResult>{
    const url = this.url.deleteByPrimaryKey + '/' + iid;
    return this.http.delete<JsonResult>(url);
  }

  selectByPrimaryKey(iid:number):Observable<JsonResult>{
    const url = this.url.selectByPrimaryKey + '/' + iid;
    return this.http.get<JsonResult>(url);
  }

  saveOne(record:ContactType):Observable<JsonResult>{
    return this.http.post<JsonResult>(this.url.saveOne,record);
  }

  all2ZOptions():Observable<ZorroOption[]>{
    return this.selectAll().pipe(
      map( res =>{
        let ret:ZorroOption[] = [];
        res.data.forEach(ele => {
          let opt:ZorroOption = {'label':ele.name,'value':ele.sid};
          ret.push(opt);
        });

        return ret;
      })
    )
  }
}

// Author：chanchaw
// Generator Version：1.0 - 2019年11月16日 16点46分
// This Model Created：Thu Dec 12 2019 13:00:36 GMT+0800 (中国标准时间)
export class ContactType{
  create_time:Date;
  iid:number;
  name:string;
  seq:number;
  sid:string;
  status:number;
}
